package com.uncp.comisiondeadmision.comprobantedepago.vista.tags;

import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.uncp.comisiondeadmision.comprobantedepago.beans.ComboBean;
import com.uncp.comisiondeadmision.comprobantedepago.dao.CombosDAO;
import com.uncp.comisiondeadmision.comprobantedepago.dao.FabricaDAO;





@SuppressWarnings("serial")
public class TagComboDinamico extends TagSupport {

	private String name,id, tipo, seleccion,onchange;

	public void setName(String name) {
		this.name = name;
	}

	public void setTipo(String tipo) {
		this.tipo = tipo;
	}

	public void setSeleccion(String seleccion) {
		this.seleccion = seleccion;
	}
	
	public void setId(String id) {
		this.id = id;
	}

	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}
	
	

	@Override
	public int doStartTag() throws JspException {
		ResourceBundle rb = ResourceBundle.getBundle("com.uncp.comisiondeadmision.comprobantedepago.vista.tags.tag_sql");
		
	
		String sql = rb.getString(tipo);
		
		
		
		FabricaDAO factoria = FabricaDAO.getSubFabrica(FabricaDAO.MYSQL);
		CombosDAO dao = factoria.getCombos();
		
		try {
			ArrayList<ComboBean> data=  dao.listaData(sql);
			JspWriter out = pageContext.getOut();
			
			out.println("<select id='"+id+"' name='" + name+ "' onchange='" +onchange +"'>");
			
			out.println("<option value='' >[Seleccione]</option>");
			if (data!= null){
				for (ComboBean x : data) {
					if(seleccion!= null && x.getStrValue().equals(seleccion))
						out.println("<option selected value='" + x.getStrValue() + "'>");
					else
						out.println("<option value='" + x.getStrValue() + "'>");
					out.println(x.getStrDescripcion());
					out.println("</option>");
				}	
			}
			out.println("</select>");
			
		} catch (Exception e) {
			System.out.println(e);
		}
	
		return super.doStartTag();
	}
}
